﻿using Eps.Ai.Common;
using InfonavaEye.handler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.util
{
    /// <summary>
    /// 焦距调节限流器
    /// （限制在一段时间内 只能最后的一次调节有效）
    /// </summary>
    public class FocusDistanceCurrentLimiter
    {
        PLCHandler plcHandler = PLCHandler.Instance;
        private object lockObj = new object();
        /// <summary>
        /// 调节中
        /// （正在调节焦距中...）
        /// </summary>
        private bool adjusting = false;

        /// <summary>
        /// 调节的目标值
        /// </summary>
        private int toFocusDistance = 0;

        public FocusDistanceCurrentLimiter()
        {
        }

        public void adjust(int value)
        {
            lock (lockObj)
            {
                if (adjusting)
                {
                    Console.WriteLine("焦距调节：无法进行，上一个正在调节中");
                    this.toFocusDistance = value;
                }
                else if (this.toFocusDistance == value)
                {
                    Console.WriteLine("焦距调节：无需调节，上一次和这一次一样");
                }
                else
                {
                    Console.WriteLine("焦距调节：开始调节...");
                    this.toFocusDistance = value;
                    sendCommond();
                }
            }
        }

        private void sendCommond()
        {
            try
            {
                this.adjusting = true;
                int currentValue = this.toFocusDistance;
                Console.WriteLine("焦距调节：保存值到配置文件中");
                Config.INI.WriteValue("device", "CAMERA_FOCUS_DISTANCE", currentValue.ToString());
                //调用硬件PLC设置参数
                plcHandler.ChangeFocusDistance(currentValue, (code, msg) =>
                {
                    this.adjusting = false;
                    if (code == 0)
                    {
                        //还存在待调节的值，继续调节到下一个值
                        if (this.toFocusDistance != currentValue)
                        {
                            Console.WriteLine("焦距调节：还存在待调节的值，继续调节到下一个值:" + this.toFocusDistance);
                            this.adjust(this.toFocusDistance);
                        }
                    }
                    else
                    {
                        MessageBox.Show(msg);
                    }
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine("焦距调节发生异常 {0} {1}", ex.Message, ex.StackTrace);
            }
            finally
            {
                //这里不要强制：this.adjusting = true，因为在上面的ChangeFocusDistance的回调之后才能改变这个值
            }
        }
    }

    /// <summary>
    /// 焦距
    /// </summary>
    public class FocusDistance
    {
        /// <summary>
        /// 焦距值
        /// </summary>
        public int value { get; set; }

        /// <summary>
        /// 时间戳
        /// </summary>
        public int timestamp { get; set; }
    }
}
